package com.hly.musicbackground.model.index.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.Final.SearchFinal;
import com.hly.musicbackground.dao.SearchKeyDao;
import com.hly.musicbackground.domain.SearchKey;
import com.hly.musicbackground.domain.Song;
import com.hly.musicbackground.exception.BusinessException;
import com.hly.musicbackground.model.boots.VO.FictionListVO;
import com.hly.musicbackground.model.boots.params.FictionQueryParams;
import com.hly.musicbackground.model.boots.service.FictionService;
import com.hly.musicbackground.model.movies.VO.MoviesListVO;
import com.hly.musicbackground.model.movies.service.MoviesService;
import com.hly.musicbackground.model.music.VO.MusicListVO;
import com.hly.musicbackground.model.index.service.SearchKeyService;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.model.music.service.impl.MusicKUWOServiceImpl;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SearchKeyServiceImpl extends ServiceImpl<SearchKeyDao, SearchKey> implements SearchKeyService {

    private final MusicKUWOServiceImpl musicKUWOService;
    private final SongService songService;
    private final FictionService fictionService;
    private final MoviesService moviesService;
    private final UserLogService userLogService;

    @Override
    public void saveOrUpdateKey(String key, String type){
        SearchKey one = lambdaQuery().eq(SearchKey::getSearchKey, key)
                .eq(SearchKey::getType, type).one();
        if (one == null){
            one = new SearchKey();
            one.setSearchKey(key);
            one.setType(type);
            save(one);
        }else{
            one.setSearchNum(one.getSearchNum() + 1);
            updateById(one);
        }
    }

    @Override
    public List<String> hotSearchKey(int limit, String type) {
        return lambdaQuery().eq(SearchKey::getType, type)
                .orderByDesc(SearchKey::getSearchNum)
            .select(SearchKey::getSearchKey).page(new Page<>(1, limit))
            .getRecords().stream().map(SearchKey::getSearchKey)
                .collect(Collectors.toList());
    }

    @Override
    public MusicListVO searchSong(String key, int page, int limit) {
        MusicListVO musicListVO = new MusicListVO();
        HashMap<String, Object> search = musicKUWOService.search(key, page, limit);
        musicListVO.setList((List<Song>)search.get("list"));
        musicListVO.setTotal((Integer) search.get("total"));
        songService.SaveList(musicListVO.getList());
        return musicListVO;
    }

    @Override
    public FictionListVO searchBooks(String key, String type, int page, int limit) {

        FictionListVO fictionListVO = null;

        FictionQueryParams build = FictionQueryParams
                .builder()
                .key(key)
                .limit(limit)
                .page(page)
                .build();

        if (SearchFinal.WHOLE.equals(type)){
            try {
                fictionListVO = fictionService.searchListDom(key);
            }catch (Exception e){
                fictionListVO = fictionService.fictionQueryList(build);
            }
        }else if(SearchFinal.FAST.equals(type)){
            fictionListVO = fictionService.fictionQueryList(build);
        }
        return fictionListVO;
    }

    @Override
    public MoviesListVO searchMovies(String key, String type, int page, int limit) {
        return moviesService.searchListDom(key);
    }

    @Override
    public Object searchKey(String aggregate, String key, String type, int page, int limit) {
        if (key == null || "".equals(key)){
            Code.PARAMS_ERR("搜索关键字不能为空");
        }
        Object vo = null;
        if (SearchFinal.TYPE_BOOK.equals(aggregate)){
            vo = searchBooks(key, type, page, limit);
        }else if (SearchFinal.TYPE_SONG.equals(aggregate)){
            vo = searchSong(key, page, limit);
        }else if (SearchFinal.TYPE_MOVIES.equals(aggregate)){
            vo = searchMovies(key, type, page, limit);
        }else{
            Code.BUSINESS_ERR("该种类不存在");
        }

        userLogService.addUserLogOrFail(0, UserLogEnum.SEARCH_KEY.getValue(), key);

        saveOrUpdateKey(key, aggregate);
        return vo;
    }
}
